package com.aha.dataanalyzeandview.utils;

import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 格式化转换
 */
@Slf4j
public class FormatUtils {

    /**
     *     获取以 #{ 开始，不是 } 结尾的多个字符 (其中()用来分组)
     */
    private final static Pattern p = Pattern.compile("#\\{([^}]+)");


    /**
     * 替换字符串中 #{} 包裹的字段的值
     * @param sourceStr  原始字符串 select * from emp where name = #{name} and id = #{id}
     * @param map   Map<String, Object> kv = new HashMap<>();   kv.put("id",1);  kv.put("name","mike");
     * @return
     */
    public static String replaceParam(String sourceStr, Map<String,Object>  map ) {
        log.info("源字符串：" + sourceStr);
        Matcher m = p.matcher(sourceStr);
        //获取 #{} 中的内容
        while (m.find()){
            Object o = map.get(m.group(1));
            if(o instanceof Integer){
                sourceStr = sourceStr.replaceFirst("#\\{[^}]+\\}",o.toString());
            }else if(o instanceof String){
                sourceStr = sourceStr.replaceFirst("#\\{[^}]+\\}","'" + o.toString() + "'");
            }
        }

        //替换#{xxx}，其中xxx是参数
        log.info("目标字符串： " + sourceStr);
        return sourceStr;
    }

//    public static void main(String[] args) {
//        String sourceStr = "select * from emp where name = #{name} and id = #{id}";
//        Map<String, Object> kv = new HashMap<>();
//        kv.put("id",1);
//        kv.put("name","mike");
//
//        System.out.println(replaceParam(sourceStr,kv));
//    }


    /**
     * 股票代码格式化成标准的字符串
     * @param src
     * @return
     */
    public static String formatStockCode (String src){
        String zeroPrifix = "";
        for (int i = 0; i < 6-src.length(); i++) {
            zeroPrifix += "0";
        }
        return zeroPrifix + src;
    }


//
//    public static void main(String[] args) {
//
//
//
//        Connection conn = null;
//        PreparedStatement ps = null;
//        ResultSet resultSet = null;
//
//        String url = String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf8", "localhost", 3306, "data_view");
//        String sql = "select * from t_connection";
//
//        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            conn = DriverManager.getConnection(url, "root", "123456");
//            ps = conn.prepareStatement(sql);
//            resultSet = ps.executeQuery();
//            while (true) {
//                if (!resultSet.next()) break;
//                log.info(resultSet.getString("user_name"));
//                log.info(resultSet.getString("pass_word"));
//            }
//            if (ps == null) {
//                ps.close();
//            }
//            if (resultSet != null) {
//                resultSet.close();
//            }
//            if (conn != null) {
//                conn.close();
//            }
//        } catch (SQLException | ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//    }

}
